function fileToBase64(path, type = 'png') {
  return new Promise((resolve, reject) => {
    // #ifdef MP-WEIXIN
    uni.getFileSystemManager().readFile({
      filePath: path, //选择图片返回的相对路径
      encoding: 'base64', //编码格式
      success: (res) => {
        resolve('data:image/' + type.toLocaleLowerCase() + ';base64,' + res.data)
      },
      fail: (res) => reject(res.errMsg),
    })
    // #endif

    // #ifdef APP-PLUS
    plus.io.resolveLocalFileSystemURL(path, function (entry) {
      entry.file(
        function (file) {
          const AppReader = new plus.io.FileReader()
          AppReader.onloadend = function (e) {
            const base64 = e.target.result
            resolve('data:image/' + type.toLocaleLowerCase() + ';base64,' + base64.split(',')[1])
          }
          AppReader.onerror = function (err) {
            reject(err)
          }
          AppReader.readAsDataURL(file)
        },
        function (e) {
          reject(e)
        }
      )
    })
    // #endif
  })
}
export default fileToBase64
